﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Zero.UControl.Component
{
    /// <summary>
    /// RollingNumber.xaml 的交互逻辑
    /// </summary>
    public partial class RollingNumberItem : INotifyPropertyChanged
    {
        private Thickness _MarginTop = new Thickness(0, 0, 0, 0);
        public Thickness MarginTop
        {
            get { return _MarginTop; }
            set { _MarginTop = value; OnPropertyChanged("MarginTop"); }
        }

        private double _Num;


        public double Num
        {
            get { return _Num; }
            set
            {
                var ease = new ExponentialEase { EasingMode = EasingMode.EaseOut };
                ThicknessAnimation animation = new ThicknessAnimation
                {
                    EasingFunction = ease,
                    From = new Thickness(0, 0 - _Num * Height, 0, 0),
                    Duration = TimeSpan.FromMilliseconds(1500)
                };

                double d = 0 - value * Height;
                if (value < _Num) d = 0 - (value + 10) * Height;

                _Num = value;
                OnPropertyChanged("Num");

                MarginTop = new Thickness(0, d, 0, 0);
                animation.To = MarginTop;

                if (animation.From != animation.To)
                    this.stackPanel.BeginAnimation(StackPanel.MarginProperty, animation);

                MarginTop = new Thickness(0, 0 - value * Height, 0, 0);
            }
        }

        public event PropertyChangedEventHandler? PropertyChanged;
        protected void OnPropertyChanged(string prop) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(prop));
    }
}
